﻿@using Easy.Web.CMS.Page
@using Easy.HTML.jsTree
@{
    string pageId = Request.QueryString["PageID"];
    Script.Reqiured("jQueryUi").AtFoot();
}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div>
                        <span class="badge">页面</span>

                        <a href="@Url.Action("Create", new { ParentID = "#" })" class="btn btn-link btn-xs">
                            <i class="glyphicon glyphicon-plus"></i>
                            添加页面
                        </a>
                    </div>
                </div>
                <div class="panel-body">

                    @(
 Html.Tree<PageEntity>().Source("GetPageTree", "Page", new { module = "Common" })
 .AddPlugin(Plugins.ContextMenu)
.AddPlugin(Plugins.DragAndDrop)
.AddContextMenuItem(new ContextmenuItem { Label = "新建", Action = "Create" })
.AddContextMenuItem(new ContextmenuItem { Label = "编辑", Action = "Edit" })
.AddContextMenuItem(new ContextmenuItem { Label = "查看", Action = "View" })
.AddContextMenuItem(new ContextmenuItem { Label = "设计", Action = "Design" })
.On(Events.Loaded, "loadedPage")
.CheckCallBack("checkCallBack")
.On(Events.MoveNode, "moveNode")
.On(Events.ActiveNode, "ActiveNode")
                    )

                </div>
            </div>
        </div>
        <div class="col-md-8" id="pageZones"></div>
    </div>
</div>

@using (Script.AtFoot())
{
    <script type="text/javascript">
        function Create(node) {
            var parent = node.reference.attr("id");
            window.location.href = '@Url.Action("Create", "Page", new { module = "Common" })' + '?ParentID=' + parent;
        }
        function Edit(node) {
            var id = node.reference.attr("id");
            window.location.href = '@Url.Action("Edit", "Page", new { module = "Common" })' + '?ID=' + id;
        }
        function View(node) {
            var id = node.reference.attr("id");
            window.location.href ='@Url.Action("RedirectView", "Page", new {module="Common" })' + '?ID=' + id;
        }
        function Design(node) {
            var id = node.reference.attr("id");
            window.location.href = '@Url.Action("Design", "Page", new {module="Common" })' + '?ID=' + id;
        }
        function ActiveNode(node, selected) {
            $.post("@Url.Action("PageZones", "Page", new { module="Common" })", { PageID: selected.node.id }, function (html) {
                $("#pageZones").html(html);
            }, "html");
        }
        function checkCallBack(operation, node, node_parent, node_position, more) {
            return operation == "move_node" && node.parent == node_parent.id;
        }
        function moveNode(node, parent) {
            $.post("@Url.Action("MovePage")", { id: parent.node.id, position: parent.position + 1, oldPosition: parent.old_position + 1 }, function () {

            }, "json");
        }
        function loadedPage(p) {
            @if (pageId.IsNotNullAndWhiteSpace())
            {
                @:$(p.target).find("a#@(pageId)").trigger("click");
            }
        }
        $(document).on("click", ".fullRowList .delete", function () {
            var id = $(this).data("widgetid");
            if (confirm("确定要删除该组件吗？")) {
                $.post("@Url.Action("DeleteWidget", "Widget",new { module="Common"})", { ID: id }, function (data) {
                    if (data) {
                        $("#widget_" + id).remove();
                    }
                }, "json");
            }
        });
    </script>
}